<?php

namespace Admin\Controller;

use Common\Controller\AdminController;

class ShopController extends AdminController {

    public function index() {
        $shopModel = new \Admin\Model\ShopAccountModel();
        $where['shop_type'] = 1;
        $shopList = $shopModel->getList($where);
        $this->shopList = $shopList['list'];
        $this->page = $shopList['page'];
        if (IS_AJAX) {
            $this->ajaxReturn($this->fetch('Shop/index_ajax'));
        } else {
            $this->display();
        }
    }

    public function addShop() {
        $shopModel = new \Admin\Model\ShopAccountModel();
        if (!IS_POST) {
            if ($_GET['id']) {
                $shop = $shopModel->getFind(array('id' => $_GET['id']));
                $shop['yytime'] = $shop['opendt'] . '-' . $shop['closedt'];
                $this->find = $shop;
            }
            $this->display();
        } else {
            $opendt = explode("-", $_POST['opendt']);
            if (empty($_POST['accountname'])) {
                $this->ajaxReturn(array('code' => 10002, 'msg' => '账号不能为空！'));
            }
            if (empty($_POST['id'])) {
                if (empty($_POST['pwd'])) {
                    $this->ajaxReturn(array('code' => 10002, 'msg' => '密码不能为空！'));
                }
            }

            if (empty($_POST['shopname'])) {
                $this->ajaxReturn(array('code' => 10002, 'msg' => '店铺名不能为空！'));
            }
            if (empty($_POST['contacts'])) {
                $this->ajaxReturn(array('code' => 10002, 'msg' => '联系人不能为空！'));
            }
            if (empty($_POST['telphone'])) {
                $this->ajaxReturn(array('code' => 10002, 'msg' => '联系电话不能为空！'));
            }

            if (empty($_POST['gps'])) {
                $this->ajaxReturn(array('code' => 10002, 'msg' => 'gps不能为空！'));
            }
            if (empty($_POST['x']) || empty($_POST['y'])) {
                $this->ajaxReturn(array('code' => 10002, 'msg' => '坐标不能为空！'));
            }
            if (empty($_POST['address'])) {
                $this->ajaxReturn(array('code' => 10002, 'msg' => '详细地址不能为空！'));
            }
            if (empty($_POST['opendt'])) {
                $this->ajaxReturn(array('code' => 10002, 'msg' => '营业时间不能为空！'));
            }
            if (empty($_POST['returndt'])) {
                $this->ajaxReturn(array('code' => 10002, 'msg' => '退货配置时长不能为空！'));
            }

            $swhere['accountname'] = $_POST['accountname'];
            $res = $shopModel->getFind($swhere);
            if ($res && $_POST['id'] != $res['id']) {
                $this->ajaxReturn(array('code' => 10003, 'msg' => '账号已被注册'));
            }
            $data['accountname'] = $_POST['accountname'];
            $data['shopname'] = $_POST['shopname'];
            $data['pwd'] = strtoupper(md5($_POST['pwd']));
            $data['telphone'] = $_POST['telphone'];
            $data['inuse'] = $_POST['inuse'];
            $data['gps'] = $_POST['gps'];
            $data['authcode'] = md5($_POST['accountname']);
            $data['x'] = $_POST['x'];
            $data['y'] = $_POST['y'];
            $data['address'] = $_POST['address'];
            $data['createdt'] = date("Y-m-d H:i:s");
            $data['updatedt'] = date("Y-m-d H:i:s");
            $data['opendt'] = $opendt[0];
            $data['closedt'] = $opendt[1];
            $data['contacts'] = $_POST['contacts'];
            $data['returndt'] = $_POST['returndt'];
            $data['shop_type'] = 1;
            if (!empty($_POST['id'])) {
                unset($data['authcode']);
                if (empty($_POST['pwd'])) {
                    unset($data['pwd']);
                }
                $result = $shopModel->saveData(array('id' => $_POST['id']), $data);
            } else {
                $result = $shopModel->addData($data);
            }

            if ($result) {
                $this->ajaxReturn(array('code' => 10000, 'msg' => '成功', 'data' => array()));
            } else {
                $this->ajaxReturn(array('code' => 10001, 'msg' => '新增错误', 'data' => array()));
            }
        }
    }

}
